// UPnP .NET Framework Device Stack, Device Module
// Device Builder Build#1.0.4561.18413

using System;
using OpenSource.UPnP;
using MediaServer;
using System.Collections.Generic;
using MediaServer.Utilities;

namespace MediaServer
{
	/// <summary>
	/// Class responsible for management of available UPnP services. Actual
    /// functionalities are provided by classes implementing IUPnPService.
	/// </summary>
	public class SimpleMediaServer
	{
        private UPnPDevice device;
        private FileServer fs;
        private ILogger logger;
        private Dictionary<String, IUPnPService> services = new Dictionary<String, IUPnPService>();

        private VirtualDirectory root = new VirtualDirectory("Root", "0", "0");

		public SimpleMediaServer(ILogger logger)
        {
            this.logger = logger;
            fs = new FileServer(12080, logger, this.root);
			device = UPnPDevice.CreateRootDevice(1800, 1.0, "\\");
			
            // Device description
			device.FriendlyName = "Simple Media Server : 1";
			device.Manufacturer = "Piotr Rzepecki";
            device.ManufacturerURL = "http://code.google.com/p/upnp-av-ms";
			device.ModelName = "Media Server";
			device.ModelDescription = "Media Server";
			device.ModelNumber = "MS1";
			device.HasPresentation = false;
			device.DeviceURN = "urn:schemas-upnp-org:device:MediaServer:1";

            // Device ConnectionManager Service
			GenericConnectionManagerService ConnectionManagerService = new MediaServer.DeviceConnectionManagerService(logger);
            services["urn:upnp-org:serviceId:ConnectionManager"] = ConnectionManagerService;
            device.AddService(ConnectionManagerService);

            // Device ContentDirectory Service
            GenericContentDirectoryService ContentDirectoryService = new MediaServer.DeviceContentDirectoryService(logger, this.root);
            services["urn:upnp-org:serviceId:ContentDirectory"] = ContentDirectoryService;
            device.AddService(ContentDirectoryService);

            // Device MS_MediaReceiverRegistrar Service
            GenericX_MS_MediaReceiverRegistrarService X_MS_MediaReceiverRegistrarService = new MediaServer.GenericX_MS_MediaReceiverRegistrarService();
            X_MS_MediaReceiverRegistrarService.External_IsAuthorized = new MediaServer.GenericX_MS_MediaReceiverRegistrarService.Delegate_IsAuthorized(X_MS_MediaReceiverRegistrarService_IsAuthorized);
            X_MS_MediaReceiverRegistrarService.External_IsValidated = new MediaServer.GenericX_MS_MediaReceiverRegistrarService.Delegate_IsValidated(X_MS_MediaReceiverRegistrarService_IsValidated);
            X_MS_MediaReceiverRegistrarService.External_RegisterDevice = new MediaServer.GenericX_MS_MediaReceiverRegistrarService.Delegate_RegisterDevice(X_MS_MediaReceiverRegistrarService_RegisterDevice);
            services["urn:upnp-org:serviceId:X_MS_MediaReceiverRegistrar"] = X_MS_MediaReceiverRegistrarService;
            device.AddService(X_MS_MediaReceiverRegistrarService);
			
			// Setting the initial value of evented variables
			X_MS_MediaReceiverRegistrarService.Evented_AuthorizationGrantedUpdateID = 0;
			X_MS_MediaReceiverRegistrarService.Evented_ValidationRevokedUpdateID = 0;
			X_MS_MediaReceiverRegistrarService.Evented_ValidationSucceededUpdateID = 0;
			X_MS_MediaReceiverRegistrarService.Evented_AuthorizationDeniedUpdateID = 0;
			ConnectionManagerService.Evented_SourceProtocolInfo = "";
			ConnectionManagerService.Evented_SinkProtocolInfo = "";
			ConnectionManagerService.Evented_CurrentConnectionIDs = "";
			ContentDirectoryService.Evented_ContainerUpdateIDs = "";
			ContentDirectoryService.Evented_SystemUpdateID = 0;
            ContentDirectoryService.Evented_X_RemoteSharingEnabled = false;
		}

        public VirtualDirectory GetRoot()
        {
            return root;
        }

        public int GetServicesCount()
        {
            return services.Count;
        }

        public IUPnPService GetService(string serviceID)
        {
            return services[serviceID];
        }
		
		public void Start()
		{
            device.StartDevice();
            logger.log("Media Server has been started.");
            fs.Start();
            logger.log("File Server has been started.");
		}
		
		public void Stop()
        {
            device.StopDevice();
            logger.log("Media Server has been stopped.");
            fs.Stop();
            logger.log("File Server has been stopped.");
		}
		
		public void X_MS_MediaReceiverRegistrarService_IsAuthorized(System.String DeviceID, out System.Int32 Result)
		{
			Result = 0;
			logger.log("X_MS_MediaReceiverRegistrarService_IsAuthorized(" + DeviceID.ToString() + ")");
		}
		
		public void X_MS_MediaReceiverRegistrarService_IsValidated(System.String DeviceID, out System.Int32 Result)
		{
			Result = 0;
            logger.log("X_MS_MediaReceiverRegistrarService_IsValidated(" + DeviceID.ToString() + ")");
		}
		
		public void X_MS_MediaReceiverRegistrarService_RegisterDevice(System.Byte[] RegistrationReqMsg, out System.Byte[] RegistrationRespMsg)
		{
			RegistrationRespMsg = System.Text.Encoding.UTF8.GetBytes("Sample String");
            logger.log("X_MS_MediaReceiverRegistrarService_RegisterDevice(" + RegistrationReqMsg.ToString() + ")");
		}
	}
}

